<?php
namespace app\api\controller;

use think\Controller;
use app\api\Util\OSSHelper;
use app\api\model\AliyunOss;

class Aliyun extends Controller
{
	/**
     * 获取oss上传签名信息
     * @return [type] [description]
     */
    public function get_upload_token($pid = '', $order_type = '1')
    {
        // 获取用户信息
        // $userInfo = getTokenInfo();
        // if (!$userInfo) 
        //     return base_result(null, false, '10001', '请登录');
        // $pid = $userInfo->user_id;
        $oss_helper = new OSSHelper;
        $result = $oss_helper->postPolicy($pid, $order_type);
        return base_result(array("oss_info" => $result));
    }

    /**
     * 阿里云oss上传后的回调函数
     * @return [type] [description]
     */
    public function post_callback()
    {
        $oss_helper = new OSSHelper;
        $result = $oss_helper->postCallback();

        if ($result['Status'] == "Ok")
        {
            header("Content-Type: application/json");
            $result['body'] = str_replace("\"", '', $result['body']);
	 		$array = explode('&', $result['body']);
	 		foreach ($array as $key => $value) {
	 			$value = explode('=', $value);
	 			$return[$value[0]] = $value[1];
	 		}
	 		// $return['fileurl'] = config('ali_cdn_url').'/'.$return['filename'];

            // $AliyunOssModel = new AliyunOss;
            // $AliyunOssModel->bucket = $return['bucket'];
            // $AliyunOssModel->name = $return['filename'];
            // // $AliyunOssModel->size = $return['size'];
            // // $AliyunOssModel->oss_type = $return['mimeType'];
            // // $AliyunOssModel->height = $return['height'];
            // // $AliyunOssModel->width = $return['width'];
            // // $AliyunOssModel->extension = $return['type'];
            // $AliyunOssModel->pid = $return['pid'];
            // $AliyunOssModel->oss_type = $return['oss_type'];
            // $AliyunOssModel->tab = $return['tab'];
            // $AliyunOssModel->add_time = date('Y-m-d H:i:s');
            // $AliyunOssModel->save();
            $ossInfo['filename'] = $return['filename'];
            return base_result($ossInfo);
        } else {
            header("http/1.1 403 Forbidden");
            exit();
        }
    }

    /**
     * 保存oss图片数据
     * @param  [type] $filename [description]
     * @param  [type] $pid      [description]
     * @return [type]           [description]
     */
    public function get_callback($filename = '', $pid = '')
    {
        if (empty($pid)) {
            return 'nopid';
        }
        $AliyunOssModel = new AliyunOss;
        $AliyunOssModel->name = $filename;
        $AliyunOssModel->pid = $pid;
        $AliyunOssModel->tab = 'billboard';
        $AliyunOssModel->add_time = date('Y-m-d H:i:s');
        $AliyunOssModel->save();
        return 'ok';
    }


}